import {Actions} from "react-native-router-flux";
import { QueryList, QueryDetail } from '../services/examination'

export default {
  namespace: 'examination',

  state: {
    list: [],
    page: 1,
    per_page: 10,
      count:0,
    currentDetail: {},
    prjname:'',
    rowData:''
  },

  subscriptions: {
  },

  effects: {
    *query({payload}, {call, put, select}){
      const { page, per_page,prjname ,rowData} = yield select(state => state.examination)
        let current = '';
        if(payload === '' || payload === undefined){
            current = page;
        }else {
            if (payload.page ==='' || payload.page === undefined){
                current = page;
            }else {
                current = payload.page;
            }
        }
        console.log(prjname,'name')
      const { json, error } = yield call(QueryList, {pageNo: current, pageSize: per_page,
                                                      "project.prjName":prjname,type:rowData})
      if(error || json.code === '0'){
        console.log('Query examinationList Error: ' + error)
      }else{
        yield put({type: 'querySuccess', payload: json.data})
      }

    },

    *queryDetail({payload}, {call, put, select}){

      const { json, error } = yield call(QueryDetail, {changeFormId: payload})

      if(error || json.code === '0'){
        console.log('Query examinationDetail Error: ' + error)
      }else{
        yield put({type: 'examinationDetail/queryDetailSuccess', payload: json.data})
        Actions.push('ExaminationDetail')
      }

    },
      *search({payload},{put,select,call}){
          yield put({type:'query',payload:{page:1}})
      },
  },

  reducers: {
    querySuccess(state, {payload}){
      console.log(payload,'-----')
      let list = payload.list.map(val => {
        let obj = {
          id: val.id,
          tableData: [
            val.prjName,
            val.seriousGrade,
            val.handleResult,
          ]
        }
        return obj
      })
      console.log(list,'ist')
      return {...state, list: list,count:payload.last,page:payload.pageNo}
    },
      thisTextChange(state,{payload}){
          return {...state,prjname:payload}
      },
      selectRowData(state,{payload}){
          if(payload === '记录'){
              payload = '0';
          }else if(payload === '整改通知单'){
              payload = '1';
          }else if(payload === '停工通知书'){
              payload = '2';
          }
          return {...state,rowData:payload}
      },
      getClean(state,{payload}){
          return {...state,prjname:'',rowData:'',page:1}
      },
  },
}